package me.zhengjie.modules.customer.service.impl;

import me.zhengjie.modules.customer.domain.TCustomerInfo;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictDetailRepository;
import me.zhengjie.modules.workorder.repository.TAccidentRepository;
import me.zhengjie.modules.workorder.repository.TWorkOrderRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.customer.repository.TCustomerInfoRepository;
import me.zhengjie.modules.customer.service.TCustomerInfoService;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoDTO;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoQueryCriteria;
import me.zhengjie.modules.customer.service.mapper.TCustomerInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author xuyukun
* @date 2019-07-21
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TCustomerInfoServiceImpl implements TCustomerInfoService {
    @Autowired
    private DictDetailRepository dictDetailRepository;
    @Autowired
    private TCustomerInfoRepository tCustomerInfoRepository;
    @Autowired
    private TCustomerInfoMapper tCustomerInfoMapper;
    @Autowired
    private TAccidentRepository tAccidentRepository;
    @Autowired
    private TWorkOrderRepository tWorkOrderRepository;

    @Override
    public Object queryAll(TCustomerInfoQueryCriteria criteria, Pageable pageable){
        Page<TCustomerInfo> page = tCustomerInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tCustomerInfoMapper::toDto));
    }

    @Override
    public Object queryAll(TCustomerInfoQueryCriteria criteria){
        return tCustomerInfoMapper.toDto(tCustomerInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TCustomerInfoDTO findById(Long id) {
        Optional<TCustomerInfo> tCustomerInfo = tCustomerInfoRepository.findById(id);
        ValidationUtil.isNull(tCustomerInfo,"TCustomerInfo","id",id);
        return tCustomerInfoMapper.toDto(tCustomerInfo.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TCustomerInfoDTO create(TCustomerInfo resources) {
        //客户分类/
        getDIctinfo(resources);
        return tCustomerInfoMapper.toDto(tCustomerInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TCustomerInfo resources) {
        Optional<TCustomerInfo> optionalTCustomerInfo = tCustomerInfoRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTCustomerInfo,"TCustomerInfo","id",resources.getId());

        TCustomerInfo tCustomerInfo = optionalTCustomerInfo.get();
        boolean flag = false;
        if(!resources.getCustomerName().equals(tCustomerInfo.getCustomerName())){
            flag = true; //需要修改名字
        }

        // 此处需自己修改
        resources.setId(tCustomerInfo.getId());
        //客户分类/
        getDIctinfo(resources);
        tCustomerInfoRepository.save(resources);

        if(flag) { //名称不一致,修改其他表里的冗余字段
            tAccidentRepository.updateCustomerName(resources.getId(), resources.getCustomerName());
            tWorkOrderRepository.updateCustomerName(resources.getId(), resources.getCustomerName());
        }
    }

    private void getDIctinfo(TCustomerInfo resources) {
        DictDetail dictDetailDTO = dictDetailRepository.findById(Long.valueOf(resources.getClassificationId())).get();
        if (dictDetailDTO != null) {
            resources.setClassificationItemName(dictDetailDTO.getLabel());
        }
        //行业分类
        DictDetail dictDetailindustryDTO = dictDetailRepository.findById(Long.valueOf(resources.getIndustryId())).get();
        if (dictDetailindustryDTO != null) {
            resources.setIndustryItemName(dictDetailindustryDTO.getLabel());
        }
        //单位性质
        DictDetail dictDetailCompanyDTO = dictDetailRepository.findById(Long.valueOf(resources.getCompanyItemId())).get();
        if (dictDetailCompanyDTO != null) {
            resources.setCompanyItemName(dictDetailCompanyDTO.getLabel());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        tCustomerInfoRepository.deleteById(id);
    }


    @Override
    public Object queryByCodeOrName(String value) {
        return tCustomerInfoRepository.findByCustomerCodeLikeOrCustomerNameLike(value,value);
    }
}